package com.ideruga.tasks;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;

import com.google.gdata.client.GoogleService;
import com.google.gdata.data.Feed;
import com.google.gdata.data.extensions.EventFeed;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;


public class MainScreenActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
        
        
        String result = "";
        try {
        	
        	final AccountManager manager = AccountManager.get(this);
            final Account[] accounts = manager.getAccountsByType("com.google");
            result = "accounts: ";
            if (accounts != null) {
            	for (Account a: accounts) {
            		result += a.name + " (name)\nCool!";
            	}
            }
            /*
                URL feedUrl = new URL("http://www.google.com/calendar/feeds/liz@gmail.com/private/full");
                result = "URL IS FINE";
                GoogleService myService = new GoogleService("cl", "exampleCo-exampleApp-1");
                result = "Got a service";
                myService.setUserCredentials("i.deruga@gmail.com", "abc");
                result = "Set up the credentials";
                */
/*
                // Mark the feed as an Event feed:
                new EventFeed().declareExtensions(myService.getExtensionProfile());

                // Send the request and receive the response:
                Feed myFeed = myService.getFeed(feedUrl, Feed.class);
                Log.i("Calendar", "Number of calendars: " + myFeed.getEntries().size());
                result = "Number: " + myFeed.getEntries().size();
                */
        } catch (Throwable t) {
                Log.e("Calendar", "Error initializing the calendar", t);
                StringWriter sw = new StringWriter();

                PrintWriter err = new PrintWriter(sw);
                if (t.getCause() != null) {
                	t.getCause().printStackTrace(err);
                } else {
                	t.printStackTrace(err);
                }
                result = "ERROR! " + sw.getBuffer().toString();

        }

       TextView tv = new TextView(this);
       tv.setText(result);
       tv.setLines(20);
       setContentView(tv);
        
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main_screen_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent = new Intent().setClass(this, MainScreenSettingsActivity.class);

		startActivity(intent);
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onOptionsMenuClosed(Menu menu) {
		// TODO Auto-generated method stub
		super.onOptionsMenuClosed(menu);
	}
    

}